public class Array2 {


    public static void main(String[] args) {

        int[][] arr = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}};


        System.out.println("普通按行遍历：");
        for (int[] a : arr) {
            for (int b : a) {
                System.out.print(b + " ");
            }
        }
        System.out.println("\r\n===============");

        System.out.println("普通按列遍历：");
        for (int j = 0; j < arr[0].length; j++) {
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i][j] + " ");
            }
        }
        System.out.println("\r\n===============");

        System.out.println("按行贪吃蛇式遍历：");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                int k = i % 2 == 0 ? j : arr[0].length - j - 1;
                System.out.print(arr[i][k] + " ");
            }
        }
        System.out.println("\r\n===============");

        System.out.println("方阵，下三角，从上往下，从左往右：");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print(arr[i][j] + " ");
            }
        }
        System.out.println("\r\n===============");

        System.out.println("方阵，下三角，从下往上，从右往左：");
        for (int i = arr.length - 1; i >= 0; i--) {
            for (int j = i; j >= 0; j--) {
                System.out.print(arr[i][j] + " ");
            }
        }
        System.out.println("\r\n===============");
    }


}
